package cn.com.aiton.common.netty.codec;

import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

public class NettyPosindaProtocol implements Serializable {
    //固定字节
    private byte protocol = (byte)0xfe;
    //协议版本号
    private byte version ;
    //保留
    private short retain;
    //
    private int len;
    //包体
    private String body;
    public static final byte PROTOCOL_FIXED = (byte)0xfe;
    //包头长度
    public static final int PACK_HEAD_LEN = 8;
    //包体最大长度
    public static final int MAX_LEN = 1000000000;
    //包体长度的字节占用长度，按协议为4个字节
    public static final int LENGTH_LEN = 4;
    //协议头的字节长度，要减去包体长度的4个字节
    public static final int PROTOOL_HAND_LEN = 4;
    public NettyPosindaProtocol() {
    }

    @Override
    public String toString() {
        return "CustomProtocol{" +
                "protocol=" + protocol +
                ", version=" + version +
                ", retain=" + retain +
                ", len=" + len +
                ", body='" + body + '\'' +
                '}';
    }

    public NettyPosindaProtocol(String body) {
        this.protocol = (byte)0xfe;
        this.version = 1;
        this.retain = 0x00;
        this.len = StringUtils.isBlank(body) ? 0 : Unpooled.copiedBuffer(body, CharsetUtil.UTF_8).readableBytes();
        this.body = body;
    }

    public byte getProtocol() {
        return protocol;
    }

    public void setProtocol(byte protocol) {
        this.protocol = protocol;
    }

    public byte getVersion() {
        return version;
    }

    public void setVersion(byte version) {
        this.version = version;
    }

    public short getRetain() {
        return retain;
    }

    public void setRetain(short retain) {
        this.retain = retain;
    }

    public int getLen() {
        return len;
    }

    public void setLen(int len) {
        this.len = len;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }
}
